'''
下拉列表控件 (QComboBox)
1. 列表添加到QComboBox
2. 获取选中的列表项。
'''

import sys
from PyQt5.QtCore import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class QComboBoxDemo(QWidget):

    def __init__(self):

        super().__init__()

        self.initUI()
        self.show()

    def initUI(self):

        self.setWindowTitle("下拉列表控件演示")
        self.resize(300, 300)


        layout = QVBoxLayout()

        self.label = QLabel('请选择编程语言')

        self.cb = QComboBox()
        self.cb.addItem('C++')
        self.cb.addItem('Python')
        self.cb.addItems(['Java', 'C#', 'Ruly'])

        self.cb.currentIndexChanged.connect(self.selectionChaned)
        layout.addWidget(self.label)
        layout.addWidget(self.cb)

        self.setLayout(layout)
    def selectionChaned(self, i):

        self.label.setText(self.cb.currentText())
        self.label.adjustSize()

        for count in range(self.cb.count()):

            print('item' + str(count) + '=' + self.cb.itemText(count))
            print('current idex', i, 'selection changed', self.cb.currentText())

class QSliderDemo(QWidget):

    def __init__(self):

        super().__init__()

        self.initUI()
        self.show()

    def initUI(self):

        self.setWindowTitle("滑块控件演示")
        self.resize(300, 300)


        layout = QVBoxLayout()

        self.label = QLabel('你好！ PyQT5')
        self.label.setAlignment(Qt.AlignCenter)

        self.slider = QSlider(Qt.Horizontal)
        self.slider.setMinimum(12)
        self.slider.setMaximum(48)
        self.slider.setSingleStep(2)
        self.slider.setValue(15)
        self.slider.setTickPosition(QSlider.TicksBelow)
        self.slider.setTickInterval(6)
        # self.slider.valueChanged.connect(lambda: self.valueChaned(self.slider))


        self.sb = QSpinBox()
        self.sb.valueChanged.connect(self.valueChaned)


        layout.addWidget(self.sb)


        layout.addWidget(self.label)
        layout.addWidget(self.slider)

        self.setLayout(layout)
    def valueChaned(self):
        print('value =', self.sb.value())
        size =  self.sb.value()
        self.label.setText(str(size))

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = QSliderDemo()
    sys.exit(app.exec_())
